const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:null,
    collectList:[],
    historyLoad:[],
    page:5,
    bjImg:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userInfo:app.globalData.userInfo,
    });
  },
  collectonList(skip,limit){//查询收藏
    const database=wx.cloud.database();
    const movieStore=database.collection("movie_store");
    movieStore.orderBy("collecttime","desc").skip(skip).limit(limit).get().then(result=>{
      console.log("查询结果=>",result);
      this.setData({
        collectList:result.data
      })
    });
  },
  loadList(skip,limit){//获取播放历史
    const database=wx.cloud.database();
    const movieStore=database.collection("movie_history");
    movieStore.orderBy("collecttime","desc").skip(skip).limit(limit).get().then(result=>{
      console.log("查询结果=loadList=>",result);
      this.setData({
        historyLoad:result.data
      })
    });
  },
  obtainImg(){
    const database=wx.cloud.database();
    const movieStore=database.collection("user_img");
    movieStore.get().then(re=>{
      // console.log("img查询结果==>",re);
      this.setData({
        bjImg:re.data[0].bjimg
      })
    })
  },
  uploadImg(){//上传背景
    wx.chooseImage({
      count: 1,
      success:(re)=>{
        this.setData({
          bjImg:re.tempFilePaths[0]
        });
        wx.cloud.uploadFile({
          cloudPath:"heade/headimg"+Math.random()+".jpg",
          filePath:re.tempFilePaths[0]
        }).then(res=>{
          wx.cloud.database().collection("user_img").add({
            data:{
              "bjimg":res.fileID,
              "username":this.data.userInfo.userInfo.nickName
            }
          }).then(re=>{
            console.log("保存成功",this.data.userInfo.userInfo.nickName,re);
            this.setData({
              bjImg:re.fileID
            })
          });
          console.log("上传成功",re)
        }).catch(er=>{
          console.log("上传失败",er)
        })
      }
    })
  },
  authUser(){//登录
    wx.getUserProfile({
      desc: '登录',
      success:(result)=>{
        console.log(result);
        app.globalData.userInfo=result;
        this.setData({
          userInfo:result
        });
        this.collectonList(0,5);
        this.loadList(0,5);
        this.obtainImg()
      },
    })
  },
  collectLoad(){//加载更多收藏列表
    wx.showLoading({
      title: '正在加载...',
    })
    let skip=++(this.data.page);
    const database=wx.cloud.database();
    const movieStore=database.collection("movie_store");
    movieStore.orderBy("collecttime","desc").skip(skip).limit(1).get().then(result=>{
      let collectList=this.data.collectList;
      collectList=collectList.concat(result.data);
      wx.hideLoading();
      this.setData({
        collectList:collectList,
      })
    })
  },
  clooectOut(){//清空收藏
    wx.cloud.callFunction({
      name:"deleteCollect",
      data:{}
    }).then(re=>{
      console.log("云函数调用==>",re);
      wx.showToast({
        title: '清除了'+re.result.stats.removed+'条数据',
      });
      this.setData({
        collectList:[]
      });
    }).catch(er=>{
      console.log(er)
    })
  },
  historyLoad(){//加载更多历史记录
    wx.showLoading({
      title: '正在加载...',
    })
    let skip=++(this.data.page);
    const database=wx.cloud.database();
    const movieStore=database.collection("movie_history");
    movieStore.orderBy("collecttime","desc").skip(skip).limit(1).get().then(result=>{
      let historyLoad=this.data.historyLoad;
      historyLoad=historyLoad.concat(result.data);
      wx.hideLoading();
      this.setData({
        historyLoad:historyLoad,
      })
    })
  },
  loadOut(){//清空历史
    wx.cloud.callFunction({
      name:"historyLoad",
      data:{}
    }).then(re=>{
      console.log("云函数histLoad调用=>",re);
      wx.showToast({
        title: '清除了'+re.result.stats.removed+'条视频',
      });
      this.setData({
        historyLoad:[]
      });
    }).catch(er=>{
      console.log(er);
    })
  },
  onPullDownRefresh(){
    this.collectonList(0,5);
    this.loadList(0,5);
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // this.setData({
    //   page:++this.data.page
    // });
    // wx.showLoading({
    //   title: '正在加载...',
    // })
    // let skip=(this.data.page-1)*5;
    // const database=wx.cloud.database();
    // const movieStore=database.collection("movie_store");
    // movieStore.orderBy("collecttime","desc").skip(skip).limit(5).get().then(result=>{
    //   let collectList=this.data.collectList;
    //   collectList=collectList.concat(result.data);
    //   wx.hideLoading();
    //   this.setData({
    //     collectList:collectList
    //   })
    // })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})